package com.ruoyi.system.service.impl.fxImpl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.domain.fx.FxNotice;
import com.ruoyi.system.mapper.SysNoticeMapper;
import com.ruoyi.system.mapper.fx.FxNoticeMapper;
import com.ruoyi.system.service.fx.IFxNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * 公告Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-15
 */
@Service
public class FxNoticeServiceImpl implements IFxNoticeService {
    @Autowired
    private FxNoticeMapper fxNoticeMapper;
    @Autowired
    private SysNoticeMapper sysNoticeMapper;

    /**
     * 查询公告
     *
     * @param id 公告主键
     * @return 公告
     */
    @Override
    public FxNotice selectFxNoticeById(Long id) {
        return fxNoticeMapper.selectFxNoticeById(id);
    }

    /**
     * 查询公告列表
     *
     * @param fxNotice 公告
     * @return 公告
     */
    @Override
    public List<FxNotice> selectFxNoticeList(FxNotice fxNotice) {
        return fxNoticeMapper.selectFxNoticeList(fxNotice);
    }

    /**
     * 公告
     * @return
     */
    public String selectNotice() {
        FxNotice fxNotice = new FxNotice();
        fxNotice.setDelFlag(1);
        fxNotice.setStatus(1);
        List<FxNotice> list = fxNoticeMapper.selectFxNoticeList(fxNotice);
        return Optional.ofNullable(list)
                .filter(n -> !n.isEmpty())
                .map(l -> l.stream()
                        .map(FxNotice::getContent)
                        .collect(Collectors.joining(";  ")))
                .orElse("");
    }

    @Override
    public String selectNoticeStr() {
        SysNotice sysNotice = new SysNotice();
        sysNotice.setStatus("0");
        List<SysNotice> list = sysNoticeMapper.selectNoticeList(sysNotice);
        return Optional.ofNullable(list)
                .filter(n -> !n.isEmpty())
                .map(l -> l.stream()
                        .map(SysNotice::getNoticeTitle)
                        .collect(Collectors.joining(";  ")))
                .orElse("");
    }

    /**
     * 新增公告
     *
     * @param fxNotice 公告
     * @return 结果
     */
    @Override
    public int insertFxNotice(FxNotice fxNotice) {
        fxNotice.setCreateBy(SecurityUtils.getUsername());
        fxNotice.setCreateTime(DateUtils.getNowDate());
        fxNotice.setUpdateTime(DateUtils.getNowDate());
        return fxNoticeMapper.insertFxNotice(fxNotice);
    }

    /**
     * 修改公告
     *
     * @param fxNotice 公告
     * @return 结果
     */
    @Override
    public int updateFxNotice(FxNotice fxNotice) {
        fxNotice.setUpdateTime(DateUtils.getNowDate());
        return fxNoticeMapper.updateFxNotice(fxNotice);
    }

    /**
     * 批量删除公告
     *
     * @param ids 需要删除的公告主键
     * @return 结果
     */
    @Override
    public int deleteFxNoticeByIds(Long[] ids) {
        return fxNoticeMapper.deleteFxNoticeByIds(ids);
    }

    /**
     * 删除公告信息
     *
     * @param id 公告主键
     * @return 结果
     */
    @Override
    public int deleteFxNoticeById(Long id) {
        return fxNoticeMapper.deleteFxNoticeById(id);
    }
}
